<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use think\Request;
use alipay\Refund;
use alipay\Query;
class Alirefund extends Controller{
    /**
     * @Author  lingyun
     * @Desc    支付宝退款
     * 2018-06-12
     * array(10) {
    ["code"] => string(5) "10000"
    ["msg"] => string(7) "Success"
    ["buyer_logon_id"] => string(14) "tia***@163.com"
    ["buyer_user_id"] => string(16) "2088902557566724"
    ["fund_change"] => string(1) "Y"
    ["gmt_refund_pay"] => string(19) "2018-06-12 14:04:57"
    ["out_trade_no"] => string(26) "20180612135159527131000001"
    ["refund_fee"] => string(4) "0.01"
    ["send_back_fee"] => string(4) "0.00"
    ["trade_no"] => string(28) "2018061221001004720560413675"
    }
     */
    public function index(){
        $order_id = input('order_id');          //订单id
        $order = db('order')->where('id',$order_id)->find();            //订单详情

        if(empty($order)){
            $this->error('找不到订单信息，不能发起退款','');
        }else if($order['is_tuikuan'] == 1){            //订单已退款
            $this->error('该订单已退款，请勿重复操作','');
        }

        $order_result = $this->search_order($order['order_sn']);

        if($order_result['code'] != 10000){
            $this->error($order_result['sub_msg'],'');
        }
        $params['trade_no'] = $order_result['trade_no'];
        $params['refund_amount'] = $order_result['total_amount'];
        $result = Refund::exec($params);

        if($result['code'] == 10000){           //退款完成
            if($order['is_tuikuan'] != 1){
                $store = db('store')->where('id',$order['store_id'])->find();
                db('order')->where('id',$order['id'])->update(['order_status'=>6,'remark'=>'订单已退款','tuikuan_time'=>time(),'is_tuikuan'=>1]);     //更新订单状态
                $order_action_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'action_user'=>session('cash_user')['id'],
                    'order_status'=>6,
                    'pay_status'=>1,
                    'status_desc'=>'订单已退款',
                    'storeid'=>$order['store_id'],
                    'addtime'=>time(),
                ];
                db('order_action')->insert($order_action_log);
                $refund_log = [
                    'uid'=>$order['uid'],
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'amount'=>$order['final_amount'],
                    'casher'=>session('cash_user')['id'],
                    'addtime'=>time()
                ];
                db('order_refund')->insert($refund_log);


                //门店金额记录
                $store_credit_log = [
                    'store_id'=>$order['store_id'],
                    'order_id'=>$order['id'],
                    'type'=>3,
                    'credit'=>$order['final_amount'],
                    'cur_credit'=>$store['total_amount']-$order['final_amount'],
                    'admin_id'=>session('cash_user')['id'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'order_type'=>1,
                    'order_sn'=>$order['order_sn'],
                    'pay_type'=>$order['pay_type']
                ];
                db('store_credit_log')->insert($store_credit_log);      //保存数据

                if ($order['pay_type'] == 1) {
                    $fild = 'al_money';
                } else if ($order['pay_type'] == 2) {
                    $fild = 'wx_money';
                } else if ($order['pay_type'] == 3) {          //余额
                    $fild = 'user_money';
                } else if ($order['pay_type'] == 4) {
                    $fild = 'xianjin_money';
                } else if ($order['pay_type'] == 5) {
                    $fild = 'bank_money';
                } else if ($order['pay_type'] == 6) {
                    $fild = 'meituan_money';
                } else if ($order['pay_type'] == 7) {
                    $fild = 'elema_money';
                }

                $update_arr = [
                    'total_amount'=>$store['total_amount']-$order['final_amount'],
                    $fild=>$store[$fild] - $order['final_amount']+$order['pay_type_two'],
                    'user_money'=>$store['user_money'] - $order['pay_type_two']
                ];
                db('store')->where('id',$store['id'])->update($update_arr);         //更新门店金额

                $pay_log = [
                    'order_id'=>$order['id'],
                    'order_sn'=>$order['order_sn'],
                    'order_type'=>empty($order['order_type'])?1:2,
                    'uid'=>$order['uid'],
                    'pay_type'=>$order['pay_type'],
                    'pay_sign'=>4,
                    'amount'=>$order['final_amount'],
                    'remark'=>'订单退款',
                    'addtime'=>time(),
                    'store_id'=>$order['store_id']
                ];
                db('pay_log')->insert($pay_log);

//                db('store')->where('id',$order['store_id'])->setInc('tuikuan_amount',$order['final_amount']);       //退款订单金额



                $this->success('退款成功','');
            }else{
                $this->success('退款成功','');
            }
        }else{
            $this->error('退款失败，请重试','');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    查询订单
     * 2018-06-12
     */
    private function search_order($sn){
        $result = Query::exec($sn);

        return $result;
    }
}